<?php

namespace Admin\Models\school;

use Admin\Models\BaseModel;
use Helpers\User;

class XHSchoolVisitType extends BaseModel
{

    public static $visitType = [1 => '公开课', 2 => '老师分享', 3 => '校园参观', 4 => '合影留念'];

    /**
     *
     * @var integer
     */
    public $iTId;

    /**
     *
     * @var integer
     */
    public $iSVId;

    /**
     *
     * @var string
     */
    public $sName;

    /**
     *
     * @var string
     */
    public $sContent;


    /**
     *
     * @var integer
     */
    public $bDelete;

    /**
     *
     * @var integer
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var integer
     */
    public $iLastModifyUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema("xh_webmanage");
        $this->setSource("XHSchool_VisitType");
        $this->belongsTo(
            'iSVId',
            'Admin\Models\school\XHSchoolVisit',
            'iId'
        );
    }


    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHSchool_VisitType';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XHSchoolVisitType[]|XHSchoolVisitType|\Phalcon\Mvc\Model\ResultSetInterface
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XHSchoolVisitType|\Phalcon\Mvc\Model\ResultInterface
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    public function beforeCreate()
    {
        $userId  = User::getId();
        $time    = date('Y-m-d H:i:s');
        $this->tCreateDate = $this->tCreateDate ?: $time;
        $this->iCreateUserId = $this->iCreateUserId ?: $userId;
        $this->tLastModifyDate = $this->tLastModifyDate ?: $time;
        $this->iLastModifyUserId = $this->iLastModifyUserId ?: $userId;
    }

    /**
     * @param null $data
     * @param null $whiteList
     * @param bool $recordUpdateInfo
     * @return bool
     */
    public function save($data = null, $whiteList = null, $recordUpdateInfo = true)
    {
        if ($recordUpdateInfo) {
            parent::beforeUpdate();
        } else {
            $this->beforeUpdate();
        }
        return parent::save($data, $whiteList);
    }

    /**
     * @return bool
     */
    public function beforeUpdate()
    {
        return true;
    }
}
